package com.evidencer.java.utils.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Title: VerifyImage.java
 * @Package com.evidencer.java.utils.servlet
 * @Description: TODO
 * @author Sunbeam lyzpoq_gmail_com
 * @date 2014-2-1 下午10:29:03
 * @version V1.0
 */
public class VerifyImage extends HttpServlet {

	private static final int HEIGHT=30;
	private static final int WIDTH=125;
	/*
	*
	Title: doGet
	*
	Description: 
	* @param req
	* @param resp
	* @throws ServletException
	* @throws IOException
	* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	*/
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		setVerifyImage(resp);
	}

	private void setVerifyImage(HttpServletResponse resp) {
		// 0.创建图像
		BufferedImage img=new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
		
		Graphics g=img.getGraphics();
		
		// 1.设置背景色
		setBackColor(g);
		
		// 2.设置边框
		setBorderLine(g);
		
		// 3.画干扰线
		drawInterferenceLine(g);
		
		// 4.写随机数
		setRamdomNum((Graphics2D)g);
		
		// 5.图形写给浏览器
		try {
			ImageIO.write(img, "jpg", resp.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	// 4.写随机数
	private void setRamdomNum(Graphics2D g) {
		g.setColor(Color.red);
		g.setFont(new Font("宋体", Font.ITALIC, 25));
		int initX=new Random().nextInt(20)+5;
		for(int i=0;i<4;i++){
			double theta=(new Random().nextInt()%10)*Math.PI/180;
			System.out.println(theta);
			g.rotate(theta);
			g.drawString(new Random().nextInt(9)+"", initX, new Random().nextInt(10)+15);
			g.rotate(-theta);
			initX=initX+new Random().nextInt(2)+23;
			
		}
		
	}

	// 3.画干扰线
	private void drawInterferenceLine(Graphics g) {
		g.setColor(Color.ORANGE);
		for(int i=0;i<5;i++){
			int x1=new Random().nextInt(WIDTH);
			int y1=new Random().nextInt(HEIGHT);
			int x2=new Random().nextInt(WIDTH);
			int y2=new Random().nextInt(HEIGHT);
			g.drawLine(x1, y1, x2, y2);
		}
		
		
	}

	//2.设置边框
	private void setBorderLine(Graphics g) {
		g.setColor(Color.GRAY);
		g.drawRect(1, 1, WIDTH-2, HEIGHT-2);
		
	}

	// 1.设置背景色
	private void setBackColor(Graphics g) {
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, WIDTH, HEIGHT);
		
		
	}

	/*
	*
	Title: doPost
	*
	Description: 
	* @param req
	* @param resp
	* @throws ServletException
	* @throws IOException
	* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	*/
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}
	

}

